<template>
	<view class="content">
		<view>
			<!-- json schema -->
			<json-editor v-if="Object.keys(schema).length" ref="JsonEditor" :schema="schema" v-model="model" readonly="readonly">
				<button @click="submit">刷新</button>
				<!-- <button @click="reset">Reset</button> -->
			</json-editor>
		</view>
	</view>
</template>

<script>
	// import vue-json-ui-editor
	import JsonEditor from 'vue-json-ui-editor';
	import JsonSchema from "@/common/json-schema/json-schema.vue";

	export default {
		components: {
			JsonEditor,
			JsonSchema
		},
		data() {
			return {
				// init json schma file ( require('@/schema/newsletter') )
				// schema: require('@/schema/tpl_test'),
				schema: {},
				// data
				model: {},
			}
		},
		created() {
			this.record_id = this.$route.query.record_id
			if (this.record_id) {
				this.getOneRecord();
			}
		},
		methods: {
			submit(_e) {
				// alert(JSON.stringify(this.model));
				location.reload();
			},
			// reset() {
			// 	this.$refs.JsonEditor.reset();
			// },
			getOneRecord() {
				uniCloud.callFunction({
					name: "ha_get_one_record",
					data: {
						record_id: this.record_id,
					},
					success: ({result}) => {
						// console.log(result);

						if ( result.status === 0 ) {
							this.schema = result.data.schema;
							this.model = result.data.model;
						} else {
							uni.showToast({
								icon: 'none',
								title: `${result.msg || "网络异常，请稍后重试！"}`,
								duration: 1000
							});
						}
					},
					fail() {
						uni.showToast({
							icon: 'none',
							title: '网络异常，请稍后重试！'
						});
					}
				});
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		margin-bottom: 50upx;
	}
</style>
